//
//  MDDEditAddressViewController.m
//  com.modada.new
//
//  Created by ferver7 on 2019/3/17.
//  Copyright © 2019 ferver7. All rights reserved.
//

#import "MDDEditAddressViewController.h"
#import "DLBaiduMapViewController.h"

@interface MDDEditAddressViewController ()
@property (weak, nonatomic) IBOutlet PPTextfield *nameTF;
@property (weak, nonatomic) IBOutlet PPTextfield *phoneTF;
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;
@property (weak, nonatomic) IBOutlet PlaceholderTextView *detailTF;
@property (weak, nonatomic) IBOutlet UIButton *selBtn;
@property (nonatomic,strong) NSString *p;//省
@property (nonatomic,strong) NSString *s;//市
@property (nonatomic,strong) NSString *a;//区
@property (nonatomic,strong) BMKPoiInfo *pointInfo;
@property (nonatomic,strong) NSString *longitude;
@property (nonatomic,strong) NSString *latitude;
@end

@implementation MDDEditAddressViewController
- (IBAction)saveAction:(id)sender {
    if (self.nameTF.text.length < 1) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请输入姓名"];
        return;
    }
    if (self.phoneTF.text.length < 1) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请输入联系电话"];
        return;
    }
    if (self.addressLabel.text.length < 1) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请选择收货地址"];
        return;
    }
    if (self.detailTF.text.length < 1) {
        [AxcUI_Toast AxcUI_showCenterWithText:@"请输入详细地址"];
        return;
    }
    NSMutableDictionary * requestInfo = [[NSMutableDictionary alloc]init];
   
    setDickeyobj(requestInfo, self.s, @"city");
    setDickeyobj(requestInfo, self.p, @"province");
    setDickeyobj(requestInfo, self.a, @"county");
    setDickeyobj(requestInfo, self.latitude, @"latitude");
    setDickeyobj(requestInfo, self.longitude, @"longitude");
    setDickeyobj(requestInfo, self.detailTF.text, @"detailsAddress");
    setDickeyobj(requestInfo, self.phoneTF.text, @"phone");
    setDickeyobj(requestInfo, self.nameTF.text, @"name");
    if (self.selBtn.selected) {
        setDickeyobj(requestInfo, @"1", @"type");
    }else{
        setDickeyobj(requestInfo, @"0", @"type");
    }
    if (self.addressModel) {
        setDickeyobj(requestInfo, self.addressModel.aid, @"aid");
    }else{
        setDickeyobj(requestInfo, @"", @"aid");
    }
    
    [PPNetworkHelper POST:app_person_address_insert parameters:requestInfo success:^(id responseObject) {
        if (YJPPSUCCESS(responseObject)) {
            [AxcUI_Toast AxcUI_showCenterWithText:@"添加地址成功"];
            [self.navigationController popViewControllerAnimated:YES];
        }else{
            [AxcUI_Toast AxcUI_showCenterWithText:responseObject[@"msg"]];
        }
    } failure:^(NSError *error) {
        
    } showLoading:self.view];
    
}
- (IBAction)moRenBtn:(id)sender {
    self.selBtn.selected = !self.selBtn.selected;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self customNavigationItems];
    mNavitationTitle(@"编辑地址");
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.detailTF.placeholderLabel.font = [UIFont systemFontOfSize:14];
    self.detailTF.placeholder = @"请输入详细地址";
    //               phtv.placeholderLabel.textAlignment = NSTextAlignmentCenter;
    self.detailTF.placeholderFrame = CGRectMake(0, 5, mScreenWidth-140, 20);
    self.detailTF.placeholderLabel.textColor = [UIColor AxcUI_colorWithHexCode:@"#B4B4B4"];
    self.detailTF.font = [UIFont systemFontOfSize:14];
    self.detailTF.maxLength = 100;
    self.detailTF.wordNumLabel.hidden = YES;
    
    self.phoneTF.isPhoneNumber = YES;
    self.phoneTF.ppTextfieldTextChangedBlock = ^(PPTextfield *tf) {
        
    };
    
    self.nameTF.maxTextLength = 20;
    self.nameTF.ppTextfieldTextChangedBlock = ^(PPTextfield *tf) {
        
    };
    if (self.addressModel) {
        self.nameTF.text = self.addressModel.name;
        self.phoneTF.text = self.addressModel.phone;
        self.p = self.addressModel.province;
        self.s = self.addressModel.city;
        self.a = self.addressModel.county;
        self.longitude = self.addressModel.longitude;
        self.latitude = self.addressModel.latltude;
        self.addressLabel.text = [NSString stringWithFormat:@"%@ %@ %@",self.p,self.s,self.a];
        self.detailTF.text = self.addressModel.detailsAddress;
        if (self.addressModel.type.integerValue == 1) {
            self.selBtn.selected = YES;
        }else{
            self.selBtn.selected = NO;
        }
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.row == 2) {
        DLBaiduMapViewController *mapVC = [[DLBaiduMapViewController alloc]init];
        [self.navigationController pushViewController:mapVC animated:YES];
        mapVC.returnSelAddress = ^(BMKPoiInfo *addressInfo, NSString *p, NSString *c, NSString *a) {
            self.pointInfo = addressInfo;
            self.longitude = [NSString stringWithFormat:@"%lf",self.pointInfo.pt.longitude];
            self.latitude = [NSString stringWithFormat:@"%lf",self.pointInfo.pt.latitude];
            
            self.p = p;
            self.s = c;
            self.a = a;
            self.addressLabel.text = [NSString stringWithFormat:@"%@ %@ %@",p,c,a];
            self.detailTF.text = addressInfo.address;
        };
    }
}

@end
